#include<iostream>
#include<cstring>
#include<string>
#include<unistd.h>
#include<pthread.h>
using std::cout;
using std::endl;
using std::string;

void TidPrint(const void* threadname,const pthread_t tid)
{
    printf("%s is running,tid::%lu,pid:;%d\n",threadname,tid,getpid());
}

void* DetachFun(void* agrc)
{
    pthread_detach(pthread_self());
    string threadname=(char*)agrc;
    int cnt=1;
    while(cnt--)
    {
        TidPrint(threadname.c_str(),pthread_self());
        sleep(1);
    }
    printf("%s has detached!\n",threadname.c_str());
    return nullptr;
}

int main()
{
    pthread_t tid1,tid2,tid3,tid4;
    pthread_create(&tid1,nullptr,DetachFun,(void*)"Thread 1");
    pthread_create(&tid2,nullptr,DetachFun,(void*)"Thread 2");
    pthread_create(&tid3,nullptr,DetachFun,(void*)"Thread 3");
    pthread_create(&tid4,nullptr,DetachFun,(void*)"Thread 4");
    
    sleep(2);

    int joinable= pthread_join(tid1,nullptr);
    cout<<strerror(joinable)<<endl;
    joinable=pthread_join(tid2,nullptr);
    cout<<strerror(joinable)<<endl;
    joinable=pthread_join(tid3,nullptr);
    cout<<strerror(joinable)<<endl;
    joinable=pthread_join(tid4,nullptr);
    cout<<strerror(joinable)<<endl;
    return 0;
}
